package com.example.dirviesystem.Admin.controller;

import com.example.dirviesystem.config.R;
import com.example.dirviesystem.entity.Question;
import com.example.dirviesystem.repository.QuestionRe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@RestController
public class QuestionController {
    @Autowired
    private QuestionRe questionRe;


    @PostMapping("/examQuestions")
    public List<Question> examQuestions(@RequestBody(required = false) String questionType) {
        String A="单选题";
        System.out.println(A);
        System.out.println(questionType);
        if (questionType == null || questionType.trim().isEmpty()) {
            return questionRe.findAll();
        } else {
            return questionRe.findByQuestionType(questionType);
        }
    }

    @PostMapping("/addQuestions")
    public R addQuestions(@RequestBody Question question) {
        questionRe.save(question);
        return  R.success("添加成功");

    }

    @PutMapping("/updateQuestions/{id}")
    public R updateQuestion(
            @PathVariable Long id,
            @RequestBody Question questionDetails){
        Optional<Question> question = questionRe.findById(id);
        if (question.isPresent()) {
            question.get().setContent(questionDetails.getContent());
            question.get().setOptionA(questionDetails.getOptionA());
            question.get().setOptionB(questionDetails.getOptionB());
            question.get().setOptionC(questionDetails.getOptionC());
            question.get().setOptionD(questionDetails.getOptionD());
            question.get().setCorrectAnswer(questionDetails.getCorrectAnswer());
            question.get().setScore(questionDetails.getScore());
            question.get().setQuestionType(questionDetails.getQuestionType());
            question.get().setSubject(questionDetails.getSubject());
            questionRe.save(question.get());
        }
        return R.success("更新成功");
    }

    @DeleteMapping("/deleteQuestions/{id}")
    public R deleteQuestion(@PathVariable Long id) {
        questionRe.deleteById(id);
        return R.success("删除成功");
    }

}
